/**
 * mask - jQuery xui
 *
 * Licensed under the GPL: http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {
	/**
     * 设置大小
     */
    function setSize(target) {
    	var state = $.data(target, 'mask');
    	if(state) {
	    	var opts = state.options;
	        var mask = state.mask;
	        var maskMsg = state.maskMsg;
	        var cc = $(target);
			
			var css = 	{
	            left: 0,
	            top: 0,
	            width: cc.outerWidth(),
	            height: cc.outerHeight()
	        };        
	        
	        if(cc[0].tagName == 'BODY') {
	        	$.extend(css, getPageArea());
	        }
	        
	        mask.css(css);
	        
	        maskMsg.css({
	            left: (mask.outerWidth() - maskMsg.outerWidth()) / 2,
	            top: (mask.outerHeight() - maskMsg.outerHeight()) / 2
	        });
	        
	        if($.fn.dialog) {
	        	mask.css('z-index', $.fn.dialog.defaults.zIndex++);
	        	maskMsg.css('z-index', $.fn.dialog.defaults.zIndex++);
	    	}
    	}
    }
    /**
     * 构建遮罩层
     */
    function buildMask(target) {
        var state = $.data(target, 'mask');
        var opts = state.options;
        var cc = $(target).css('zoom','1'); //ie6 bug
        
        var mask = $('<div class="mask"></div>').css('display', 'block').appendTo(cc);
        var maskMsg = $('<div class="mask-msg"></div>').html(opts.msg).css('display', 'block').appendTo(cc);

        state.mask = mask;
        state.maskMsg = maskMsg;
        
        setSize(target);
    }
    
    /**
     * 绑定事件
     */
    function bindMaskEvents(target) {
        $(window).resize(function() {
           setSize(target); //resize时mask可能已经destroy了
        });
    }

    /**
     * 显示遮罩层
     */
    function show(target) {
        var state = $.data(target, 'mask');
       	if(state) {
	        var mask = state.mask;
	        var maskMsg = state.maskMsg;
	        $('select:not([disabled])', target).addClass('mask-disabled-select').attr('disabled', 'disabled'); //ie6下的select的z-index bug
	        mask.css('display', 'block');
	        maskMsg.css('display', 'block');
	        setSize(target);
	        state.showCount++;
    	}
    }

    /**
     * 隐藏遮罩层
     */
    function hide(target) {
        var state = $.data(target, 'mask');
        if (state) {
        	state.showCount--;
        	if(state.showCount <= 0) {
	            var mask = state.mask;
	            var maskMsg = state.maskMsg;
	            $('select.mask-disabled-select').removeAttr('disabled');
	            mask.css('display', 'none');
	            maskMsg.css('display', 'none');
        	}
        }
    }

    /**
     * 销毁遮罩层
     */
    function destroy(target) {
        var state = $.data(target, 'mask');
        if (state) {
            var mask = state.mask;
            var maskMsg = state.maskMsg;
            $('select.mask-disabled-select').removeClass('mask-disabled-select').removeAttr('disabled');
            mask.remove();
            maskMsg.remove();
            $(target).removeData('mask');
        }
    }
	
	function getPageArea() {
        return {
            width: Math.max(document.body.clientWidth, document.documentElement.clientWidth),
            height: Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        }
    }
    
    /**
     * 遮罩层
     */
    $.fn.mask = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.mask.methods[options](this, param);
        }
        options = options || {};

        return this.each(function() {
            var state = $.data(this, 'mask');
            var opts;
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.mask.defaults, options);
                $.data(this, 'mask', {
                	showCount: 0,
                    options: opts
                });

                buildMask(this);
            }

            if (opts.hide) {
                hide(this);
            } else {
                show(this);
            }

            bindMaskEvents(this);
        });
    };

    $.fn.mask.methods = {
        show: function(jq) {
            jq.each(function() {
                show(this);
            });
        },
        hide: function(jq) {
            jq.each(function() {
                hide(this);
            });
        },
        destroy: function(jq) {
            jq.each(function() {
                destroy(this);
            });
        }
    }

    $.fn.mask.defaults = {
        msg: '处理中，请稍候',
        hide: false
    }
})(jQuery);
